//
//  WH_ClassListVC.m
//  App_6.23
//
//  Created by wanghao on 16/7/4.
//  Copyright © 2016年 wanghao. All rights reserved.
//

#import "WH_ClassListVC.h"
#import "WH_ClassListCell.h"
#import "WH_Classification.h"
#import "WH_HomeDetailController.h"

@interface WH_ClassListVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isUploading;
@property (nonatomic, strong) UILabel *countLabel;

@end

@implementation WH_ClassListVC

-(void)viewWillAppear:(BOOL)animated{
    
    
    [self.tabBarController.tabBar setHidden:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
    
    
    [self.tabBarController.tabBar setHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArr = [NSMutableArray array];
    [self createTab];
    [self createLabel];
    [self headerTab];
    [self createHub];
    [self hubShow];
}
#pragma mark - 创建视图

-(void)createLabel{
    
    self.countLabel = [UILabel new];
    _countLabel.backgroundColor = [UIColor whiteColor];
    _countLabel.font = [UIFont systemFontOfSize:14.f];
    [self.view addSubview:_countLabel];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(@0);
        make.top.mas_equalTo(@0);
        make.height.mas_equalTo(@40);
    }];

    UILabel *label = [UILabel new];
    label.backgroundColor = [UIColor colorWithRed:0.67 green:0.67 blue:0.67 alpha:1];
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(@0);
        make.top.equalTo(self.countLabel.mas_bottom);
        make.height.mas_equalTo(@1);
    }];

    
}
-(void)createTab{
    
    self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.rowHeight = 100;
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_myTableView];

    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.top.mas_equalTo(@40);
        
    }];
    [self.myTableView registerClass:[WH_ClassListCell class] forCellReuseIdentifier:@"classList"];
    
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerTab)];
    self.myTableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerTab)];
    self.myTableView.mj_footer = footer;
    
    [self.myTableView setHidden:YES];
}
#pragma mark - 刷新&&加载

-(void)headerTab{
    
    self.page = 1;
    self.isUploading = NO;
    [self postData];
}
-(void)footerTab{
    
    self.page++;
    self.isUploading = YES;
    [self postData];
    
}
#pragma mark - postData

-(void)postData{
    
    NSString *body = [NSString stringWithFormat:@"action=product_list&category_id=%@&page=%ld",self.categoryID,self.page];
    [WH_AppTools postUrl:URLstr body:body result:HXJSON requestStyle:(HXRequestString) headerFile:nil success:^(id result) {
        if (self.isUploading == NO) {
            
            [self.dataArr removeAllObjects];
        }
        NSLog(@"classList==%@",result);
        NSDictionary *dic = result;
        NSString *status = [dic objectForKey:@"product_quantity"];
        NSInteger countSta = [status integerValue];
        NSArray *productArr = [dic objectForKey:@"product_array"];
        
        for (NSDictionary *dic in productArr) {
            
            WH_Classification *listClass = [WH_Classification modelWithDictionary:dic];
            [self.dataArr addObject:listClass];
        }
        
        [self.myTableView reloadData];
        [self.myTableView setHidden:NO];
        [self hubDismiss];
        if (countSta == 0) {
            
            self.myTableView.mj_footer.state = MJRefreshStateNoMoreData;
        }else{
            
            
            [self endFresh];
        }
        
        self.countLabel.text = [NSString stringWithFormat:@"  当前共%ld件商品",(unsigned long)self.dataArr.count];
    } failure:^(NSError *error) {
        
        
    }];

    
}

#pragma mark - 结束刷新

-(void)endFresh{
    
    [self.myTableView.mj_footer endRefreshing];
    [self.myTableView.mj_header endRefreshing];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *classList = @"classList";
    WH_ClassListCell *cell = [tableView dequeueReusableCellWithIdentifier:classList];
    cell.listC = self.dataArr[indexPath.row];
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WH_HomeDetailController *detailVC = [[WH_HomeDetailController alloc] init];
    WH_Classification *product = self.dataArr[indexPath.row];
    detailVC.productID = product.product_id;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
